<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
    String path = request.getContextPath();
    // 获得项目完全路径（假设你的项目叫MyApp，那么获得到的地址就是 http://localhost:8080/MyApp/）:
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
    <title>权限</title>
    <jsp:include page="/common/backend_common.jsp"/>
    <jsp:include page="/common/page.jsp"/>
</head>
<body class="no-skin" youdao="bind" style="background: white">
<input id="gritter-light" checked="" type="checkbox" class="ace ace-switch ace-switch-5"/>

<div class="page-header">
    <h1>
        工作流默认审核管理
        <small>
            <i class="ace-icon fa fa-angle-double-right"></i>
            维护工作流关卡与审核人的关系
        </small>
    </h1>
</div>
<div class="main-content-inner">
    <div class="col-sm-3">
        <div class="table-header">

            工作流列表&nbsp;&nbsp;
            <a class="green" href="#">
                <i class="ace-icon fa fa-plus-circle orange bigger-130 workFlow-add"></i>
            </a>
        </div>
        <div id="workFlowList">
        </div>
    </div>
    <div class="col-sm-2">
        <div class="table-header">

            关卡列表&nbsp;&nbsp;
            <a class="green" href="#">
                <i class="ace-icon fa fa-plus-circle orange bigger-130 status-add"></i>
            </a>
        </div>
        <div id="statusList">
        </div>
    </div>
    <div class="col-sm-7">
        <div class="col-xs-12">
            <div class="table-header">
                关卡标准配置列表&nbsp;&nbsp;
                <a class="green" href="#">
                    <i class="ace-icon fa fa-plus-circle orange bigger-130 criterion-add"></i>
                </a>
            </div>
            <div>
                <div id="dynamic-table_wrapper" class="dataTables_wrapper form-inline no-footer">
                    <div class="row">
                        <div class="col-xs-6">
                            <div class="dataTables_length" id="dynamic-table_length"><label>
                                展示
                                <select id="pageSize" name="dynamic-table_length" aria-controls="dynamic-table" class="form-control input-sm">
                                    <option value="10">10</option>
                                    <option value="25">25</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select> 条记录 </label>
                            </div>
                        </div>
                    </div>
                    <table id="dynamic-table" class="table table-striped table-bordered table-hover dataTable no-footer" role="grid"
                           aria-describedby="dynamic-table_info" style="font-size:14px">
                        <thead>
                        <tr role="row">
                            <th tabindex="0" aria-controls="dynamic-table" rowspan="1" colspan="1">
                                标准名称
                            </th>
                            <th tabindex="0" aria-controls="dynamic-table" rowspan="1" colspan="1">
                                审核人
                            </th>
                            <th tabindex="0" aria-controls="dynamic-table" rowspan="1" colspan="1">
                                条件
                            </th>
                            <%--<th tabindex="0" aria-controls="dynamic-table" rowspan="1" colspan="1">--%>
                                <%--URL--%>
                            <%--</th>--%>
                            <th tabindex="0" aria-controls="dynamic-table" rowspan="1" colspan="1">
                                状态
                            </th>
                            <th class="sorting_disabled" rowspan="1" colspan="1" aria-label=""></th>
                        </tr>
                        </thead>
                        <tbody id="criterionList"></tbody>
                    </table>
                    <div class="row" id="criterionPage">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="dialog-workFlow-form" style="display: none;">
    <form id="workFlowForm">
        <table class="table table-striped table-bordered table-hover dataTable no-footer" role="grid">
            <tr>
                <td><label for="workFlowName">名称</label></td>
                <td>
                    <input type="text" name="name" id="workFlowName" value="" class="text ui-widget-content ui-corner-all">
                    <input type="hidden" name="id" id="workFlowId"/>
                </td>
            </tr>
            <tr>
                <td><label for="workFlowStatus">状态</label></td>
                <td>
                    <select id="workFlowStatus" name="status" data-placeholder="状态" style="width: 150px;">
                        <option value="1">可用</option>
                        <option value="0">冻结</option>
                    </select>
                </td>
            </tr>
            <td><label for="workFlowRemark">备注</label></td>
            <td><textarea name="remark" id="workFlowRemark" class="text ui-widget-content ui-corner-all" rows="3" cols="25"></textarea></td>
            </tr>
        </table>
    </form>
</div>
<div id="dialog-status-form" style="display: none;">
    <form id="statusForm">
        <table class="table table-striped table-bordered table-hover dataTable no-footer" role="grid">
            <tr>
                <td style="width: 80px;"><label for="parentId">所属工作流</label></td>
                <td>
                    <select id="parentId" name="workFlowId" data-placeholder="选择工作流" style="width: 200px;"></select>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="statusName">名称</label>
                    <input type="hidden" name="id" id="statusId"/>
                </td>
                <td><input type="text" name="name" id="statusName" value="" class="text ui-widget-content ui-corner-all"></td>
            </tr>
            <tr>
                <td><label for="statusSeq">顺序</label></td>
                <td><input type="text" name="seq" id="statusSeq" value="1" class="text ui-widget-content ui-corner-all"></td>
            </tr>
            <tr>
                <td><label for="statusStatus">状态</label></td>
                <td>
                    <select id="statusStatus" name="status" data-placeholder="状态" style="width: 150px;">
                        <option value="1">可用</option>
                        <option value="0">冻结</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label for="statusRemark">备注</label></td>
                <td><textarea name="remark" id="statusRemark" class="text ui-widget-content ui-corner-all" rows="3" cols="25"></textarea></td>
            </tr>
        </table>
    </form>
</div>

<div id="dialog-criterion-form" style="display: none;">
    <form id="criterionForm">
        <table class="table table-striped table-bordered table-hover dataTable no-footer" role="grid">
            <tr>
                <td style="width: 80px;"><label for="statusParentId">所属关卡</label></td>
                <td>
                    <select id="statusParentId" name="statusId" data-placeholder="选择关卡" style="width: 200px;"></select>
                </td>
            </tr>
            <tr>
                <td><label for="criterionName">关卡名称</label></td>
                <input type="hidden" name="id" id="criterionId"/>
                <td><input type="text" name="name" id="criterionName" value="" class="text ui-widget-content ui-corner-all"></td>
            </tr>
            <tr>
                <td><label class="col-sm-2 control-label" for="criterionApprover">审核者</label></td>
                <td>
                    <select id="criterionApprover" name="approver"  class="selectpicker" multiple data-live-search="true" data-selected-text-format="count > 3" cols="5">
                        <option value="1">广东省</option>
                        <option value="2">广西省</option>
                        <option value="3">福建省</option>
                        <option value="4">湖南省</option>
                        <option value="5">山东省</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label for="conditions">条件</label></td>
                <td><textarea type="text" name="conditions" id="conditions" placeholder="如'字段:条件;价格:[10,20);数量:30;时间:>2018-05-21 12:05'" class="text ui-widget-content ui-corner-all"  rows="3"  cols="35"></textarea></td>
            </tr>
            <tr>
                <td><label for="criterionStatus">状态</label></td>
                <td>
                    <select id="criterionStatus" name="status" class="selectpicker" data-placeholder="选择状态" style="width: 150px;">
                        <option value="1">有效</option>
                        <option value="0">无效</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label for="criterionRemark">备注</label></td>
                <td><textarea name="remark" id="criterionRemark"  class="text ui-widget-content ui-corner-all" rows="3" cols="25"></textarea></td>
            </tr>
        </table>
    </form>
</div>

<script id="workFlowListTemplate" type="x-tmpl-mustache">
<ol class="dd-list">
    {{#workFlowList}}
        <li class="dd-item dd2-item workFlow-name" id="workFlow_{{id}}" href="javascript:void(0)" data-id="{{id}}">
            <div class="dd2-content" style="cursor:pointer;">
            {{name}}
            <span style="float:right;">
                <a class="green workFlow-edit" href="#" data-id="{{id}}" >
                    <i class="ace-icon fa fa-pencil bigger-100"></i>
                </a>
                &nbsp;
                <a class="red workFlow-delete" href="#" data-id="{{id}}" data-name="{{name}}">
                    <i class="ace-icon fa fa-trash-o bigger-100"></i>
                </a>
            </span>
            </div>
        </li>
    {{/workFlowList}}
</ol>
</script>

<script id="statusListTemplate" type="x-tmpl-mustache">
<ol class="dd-list ">
    {{#statusList}}
        <li class="dd-item dd2-item status-name" id="status_{{id}}" href="javascript:void(0)" data-id="{{id}}">
            <div class="dd2-content" style="cursor:pointer;">
            {{name}}
            <span style="float:right;">
                <a class="green status-edit" href="#" data-id="{{id}}" >
                    <i class="ace-icon fa fa-pencil bigger-100"></i>
                </a>
                &nbsp;
                <a class="red status-delete" href="#" data-id="{{id}}" data-name="{{name}}">
                    <i class="ace-icon fa fa-trash-o bigger-100"></i>
                </a>
            </span>
            </div>
        </li>
    {{/statusList}}
</ol>
</script>

<script id="criterionListTemplate" type="x-tmpl-mustache">
{{#criterionList}}
<tr role="row" class="criterion-name odd" data-id="{{id}}"><!--even -->
    <td><a href="#" class="criterion-edit" data-id="{{id}}">{{name}}</a></td>
    <%--<td>{{showStatusName}}</td>--%>
    <td>{{approver}}</td>
    <td>{{conditions}}</td>
    <td>{{#bold}}{{showStatus}}{{/bold}}</td>
    <%--<td>{{seq}}</td>--%>
    <td>
        <div class="hidden-sm hidden-xs action-buttons">
            <a class="green criterion-edit" href="#" data-id="{{id}}">
                <i class="ace-icon fa fa-pencil bigger-100"></i>
            </a>
            <a class="red criterion-role" href="#" data-id="{{id}}">
                <i class="ace-icon fa fa-flag bigger-100"></i>
            </a>
        </div>
    </td>
</tr>
{{/criterionList}}
</script>

<script type="text/javascript">
    $(function () {

        //缓存map
        var workFlowMap = {};
        //缓存关卡
        var statusMap = {};
        //缓存关卡标准
        var criterionMap = {};
        //换成关卡
        //最近点击的工作流ID
        var lastWorkFlowId = -1;
        //最近点击的关卡
        var lastStatusId = -1;

        var workFlowListTemplate = $("#workFlowListTemplate").html();
        Mustache.parse(workFlowListTemplate);
        var statusListTemplate = $("#statusListTemplate").html();
        Mustache.parse(statusListTemplate);
        var criterionListTemplate = $("#criterionListTemplate").html();
        Mustache.parse(criterionListTemplate);


        loadWorkFlowList();

        //加载工作流集合
        function loadWorkFlowList() {
            $.ajax({
                url: "plm/workFlow/list.json",
                success: function (result) {
                    if(result.ret){
                        //渲染工作流数据
                        var rendered = Mustache.render(workFlowListTemplate, {workFlowList: result.data});
                        $("#workFlowList").html(rendered);
                        //放入缓存中
                        for (var i = 0; i < result.data.length; i++) {
                            var workFlow = result.data[i];
                            workFlowMap[workFlow.id] = workFlow;
                        }
                        bindWorkFlowClick();
                    }else {
                        showMessage("加载工作流", false, result.msg);
                    }
                }
            })
        }

        $(".workFlow-add").click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            $("#dialog-workFlow-form").dialog({
                model: true,
                title: "新增工作流",
                open: function (event, ui) {
                    $(".ui-dialog-titlebar-close", $(this).parent()).hide();
                    $("#workFlowForm")[0].reset();
                },
                buttons: {
                    "添加": function (e) {
                        e.preventDefault(); //阻止默认效果
                        updateWorkFlow(true, function (data) {
                            $("#dialog-workFlow-form").dialog("close"); //关闭窗口
                        }, function (data) {
                            showMessage("新增工作流", data.msg, false);
                        })
                    },
                    "取消": function (e) {
                        $("#dialog-workFlow-form").dialog("close"); //关闭窗口
                    }
                }
            });
        })

        function updateWorkFlow(isCreate, successCallback, failCallback) {
            $.ajax({
                url: isCreate ? "plm/workFlow/save.json" : "plm/workFlow/update.json",
                data: $('#workFlowForm').serializeArray(),
                type: 'POST',
                success: function (result) {
                    if(result.ret) {
                        //成功后渲染
                        loadWorkFlowList();
                        if (successCallback) {
                            successCallback(result);
                        }
                    }else {
                        if (failCallback) {
                            failCallback(result);
                        }
                    }
                }
            })
        }

        function bindWorkFlowClick() {
            $(".workFlow-edit").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var workFlowId = $(this).attr("data-id");
                $("#dialog-workFlow-form").dialog({
                    model: true,
                    title: "编辑工作流",
                    open: function(event, ui) {
                        $(".ui-dialog-titlebar-close", $(this).parent()).hide(); //取消自带的关闭
                        $("#workFlowForm")[0].reset();
                        var targetWorkFlow = workFlowMap[workFlowId];
                        if (targetWorkFlow) {
                            $("#workFlowId").val(targetWorkFlow.id);
                            $("#workFlowName").val(targetWorkFlow.name);
                            $("#workFlowStatus").val(targetWorkFlow.status);
                            $("#workFlowRemark").val(targetWorkFlow.remark);
                        }
                    },
                    buttons: {
                        "更新": function (e) {
                            e.preventDefault(); //阻止默认效果
                            updateWorkFlow(false, function (data) {
                                $("#dialog-workFlow-form").dialog("close"); //关闭窗口
                            }, function (data) {
                                showMessage("编辑工作流", data.msg, false);
                            })
                        },
                        "取消": function (e) {
                            $("#dialog-workFlow-form").dialog("close");
                        }
                    }
                })
            })
            //点击工作流名称
            $(".workFlow-name").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var workFlowId = $(this).attr("data-id");
                var workFlowName = $(this).attr("data-name");

                //处理选中的效果及工作流关卡的展示
                handleWorkFlowSelected(workFlowId);
            })
            //删除工作流
            $(".workFlow-delete").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var workFlowId = $(this).attr("data-id");
                var workFlowName = $(this).attr("data-name");
                $.alertable.confirm("确认要删除工作流[" + workFlowName + "]吗？").then(function() {
                    $.ajax({
                        url: "plm/workFlow/delete.json",
                        type: "POST",
                        data: {
                            workFlowId: workFlowId
                        },
                        success: function (result) {
                            if(result.ret) {
                                $.alertable.alert("工作流[" + workFlowName + "]删除成功");
                                loadWorkFlowList();
                            }else {
                                $.alertable.alert("删除失败:"+result.msg);
                            }
                        }
                    })
                });
            })

        }

        function handleWorkFlowSelected(workFlowId) {
            if(lastWorkFlowId != -1) {
                var lastWorkFlow = $("#workFlow_" + lastWorkFlowId + " .dd2-content:first");
                lastWorkFlow.removeClass("btn-yellow");
                lastWorkFlow.removeClass("no-hover");
            }
            var currentWorkFlow = $("#workFlow_" + workFlowId + " .dd2-content:first");
            currentWorkFlow.addClass("btn-yellow");
            currentWorkFlow.addClass("no-hover");
            lastWorkFlowId = workFlowId;
            loadWorkFlowStatusList(workFlowId);
        }

        function loadWorkFlowStatusList(workFlowId) {
            var url = 'plm/workFlowStatus/list.json?workFlowId=' + workFlowId;
            $.ajax({
                url: url,
                success: function (result) {
                    if(result.ret){
                        //渲染工作流数据
                        var rendered = Mustache.render(statusListTemplate, {statusList: result.data});
                        $("#statusList").html(rendered);
                        //放入缓存中
                        for (var i = 0; i < result.data.length; i++) {
                            var status = result.data[i];
                            statusMap[status.id] = status;
                        }
                        bindWorkFlowStatusClick();
                    }else {
                        showMessage("加载关卡", false, result.msg);
                    }
                }
            });
        }

        $(".status-add").click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            if(lastWorkFlowId == -1){
                $.alertable.alert("请先选择工作流");
                return;
            }
            $("#dialog-status-form").dialog({
                model: true,
                title: "新增关卡",
                open: function (event, ui) {
                    $(".ui-dialog-titlebar-close", $(this).parent()).hide();
                    var currentWorkFlow = workFlowMap[lastWorkFlowId];
                    optionStr = Mustache.render("<option value='{{id}}'>{{name}}</option>", {id: currentWorkFlow.id, name: currentWorkFlow.name});
                    $("#statusForm")[0].reset();
                    $("#parentId").html(optionStr);
                    //$("#parentId").attr("disabled","disabled");
                },
                buttons: {
                    "添加": function (e) {
                        e.preventDefault(); //阻止默认效果
                        updateWorkFlowStatus(true, function (data) {
                            $("#dialog-status-form").dialog("close"); //关闭窗口
                        }, function (data) {
                            showMessage("新增关卡", data.msg, false);
                        },lastWorkFlowId)
                    },
                    "取消": function (e) {
                        $("#dialog-status-form").dialog("close"); //关闭窗口
                    }
                }
            });
        })

        function updateWorkFlowStatus(isCreate, successCallback, failCallback, lastWorkFlowId) {
            $.ajax({
                url: isCreate ? "plm/workFlowStatus/save.json" : "plm/workFlowStatus/update.json",
                data: $('#statusForm').serializeArray(),
                type: 'POST',
                success: function (result) {
                    if(result.ret) {
                        //成功后渲染
                        loadWorkFlowStatusList(lastWorkFlowId);
                        if (successCallback) {
                            successCallback(result);
                        }
                    }else {
                        if (failCallback) {
                            failCallback(result);
                        }
                    }
                }
            })
        }

        function bindWorkFlowStatusClick() {
            $(".status-edit").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var statusId = $(this).attr("data-id");
                $("#dialog-status-form").dialog({
                    model: true,
                    title: "编辑关卡",
                    open: function(event, ui) {
                        $(".ui-dialog-titlebar-close", $(this).parent()).hide(); //取消自带的关闭
                        $("#statusForm")[0].reset();
                        var currentWorkFlow = workFlowMap[lastWorkFlowId];
                        optionStr = Mustache.render("<option value='{{id}}'>{{name}}</option>", {id: currentWorkFlow.id, name: currentWorkFlow.name});
                        $("#parentId").html(optionStr);
                        var targetWorkFlowStatus = statusMap[statusId];
                        if (targetWorkFlowStatus) {
                            $("#statusId").val(targetWorkFlowStatus.id);
                            $("#statusName").val(targetWorkFlowStatus.name);
                            $("#statusSeq").val(targetWorkFlowStatus.seq);
                            $("#statusRemark").val(targetWorkFlowStatus.remark);
                            $("#statusStatus").val(targetWorkFlowStatus.status);
                        }
                    },
                    buttons: {
                        "更新": function (e) {
                            e.preventDefault(); //阻止默认效果
                            updateWorkFlowStatus(false, function (data) {
                                $("#dialog-status-form").dialog("close"); //关闭窗口
                            }, function (data) {
                                showMessage("编辑关卡", data.msg, false);
                            }, lastWorkFlowId)
                        },
                        "取消": function (e) {
                            $("#dialog-status-form").dialog("close");
                        }
                    }
                })
            })
            $(".status-delete").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var workFlowStatusId = $(this).attr("data-id");
                var workFlowStatusName = $(this).attr("data-name");
                $.alertable.confirm("确认要删除关卡[" + workFlowStatusName + "]吗？").then(function() {
                    $.ajax({
                        url: "plm/workFlowStatus/delete.json",
                        type: "POST",
                        data: {
                            workFlowStatusId: workFlowStatusId
                        },
                        success: function (result) {
                            if(result.ret) {
                                $.alertable.alert("关卡[" + workFlowStatusName + "]删除成功");
                                loadWorkFlowStatusList(lastWorkFlowId);
                            }else {
                                $.alertable.alert("删除失败:"+result.msg);
                            }
                        }
                    })
                });
            })
            //点击关卡名称
            $(".status-name").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var statusId = $(this).attr("data-id");
                var statusName = $(this).attr("data-name");
                //处理选中的效果及工作流关卡的展示
                handleWorkFlowStatusCriterionSelected(statusId);
            })
        }

        function handleWorkFlowStatusCriterionSelected(statusId) {
            if(lastStatusId != -1) {
                var lastWorkFlowStatus = $("#status_" + lastStatusId + " .dd2-content:first");
                lastWorkFlowStatus.removeClass("btn-yellow");
                lastWorkFlowStatus.removeClass("no-hover");
            }
            var currentWorkFlowStatus = $("#status_" + statusId + " .dd2-content:first");
            currentWorkFlowStatus.addClass("btn-yellow");
            currentWorkFlowStatus.addClass("no-hover");
            lastStatusId = statusId;
            loadWorkFlowStatusCriterionList(statusId);
        }

        //加载关卡标准列表
        function loadWorkFlowStatusCriterionList(statusId) {
            var url = 'plm/workFlowStatusCriterion/page.json?statusId=' + statusId;
            //需要显示的当前页行数
            var pageSize = $("#pageSize").val();
            //当前页码
            var pageNo = $("#criterionPage .pageNo").val() || 1;
            $.ajax({
                url: url,
                data: {
                    pageSize: pageSize,
                    pageNo: pageNo
                },
                success: function (result) {
                    renderStatusCriterionListAndPage(result, pageSize, pageNo, url);
                }
            });
        }

        function renderStatusCriterionListAndPage(result, pageSize, pageNo, url) {
            if (result.ret) {
                if (result.data.total > 0) {
                    var rendered = Mustache.render(criterionListTemplate, {
                        criterionList: result.data.data,
                        "showStatus" : function () {
                            return this.status == 1 ? "有效" : "无效";
                        },
                        "bold": function () {
                            return function (text, render) {
                                var status = render(text); //渲染状态
                                if(status == '有效') {
                                    return "<span class='label label-sm label-success'>有效</span>"
                                } else if (status == '无效'){
                                    return "<span class='label label-sm label-warning'>无效</span>"
                                } else {
                                    return "<span class='label'>删除</span>"
                                }
                            }
                        }
                    });
                    $("#criterionList").html(rendered);
                    //将每个关卡标准对象放入criterionMap作为缓存
                    for (var i = 0; i<result.data.data.length; i++) {
                        var criterion = result.data.data[i];
                        criterionMap[criterion.id] = criterion;
                    }
                    console.log(criterionMap)
                    //绑定点击方法
                    bindStatusCriterionClick();
                    //分页信息的设置及页码超链接的设置
                    renderPage(url, result.data.total, pageNo, pageSize,
                        result.data.total > 0 ? result.data.data.length : 0, "criterionPage", renderStatusCriterionListAndPage);
                }else {
                    $("#criterionList").html("");
                }

            }else {
                showMessage("加载关卡标准信息", false, result.msg);
            }
        }

        function bindStatusCriterionClick() {
            $(".criterion-edit").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                if(lastStatusId == -1){
                    $.alertable.alert("请先选择关卡");
                    return;
                }
                var criterionId = $(this).attr("data-id");
                console.log(criterionMap)
                var targetCriterion = criterionMap[criterionId];
                console.log(criterionId+","+targetCriterion);
                alert(123);
                $("#dialog-criterion-form").dialog({
                    model: true,
                    title: "修改关卡标准",
                    open: function (event, ui) {
                        $(".ui-dialog-titlebar-close", $(this).parent()).hide();
                        var currentWorkFlowStatus = statusMap[lastStatusId];
                        optionStr = Mustache.render("<option value='{{id}}'>{{name}}</option>", {id: currentWorkFlowStatus.id, name: currentWorkFlowStatus.name});
                        $("#statusParentId").html(optionStr);
                        var targetCriterion = criterionMap[criterionId];
                        console.log(targetCriterion);
                        if (targetCriterion) {
                            $("#criterionId").val(targetCriterion.id);
                            $("#criterionName").val(targetCriterion.name);
                            $("#criterionApprover").val(targetCriterion.approver);
                            $("#conditions").val(targetCriterion.conditions);
                            $("#criterionStatus").val(targetCriterion.status);
                            $("#criterionRemark").val(targetCriterion.remark);
                        }
                        //$("#parentId").attr("disabled","disabled");
                    },
                    buttons: {
                        "添加": function (e) {
                            e.preventDefault(); //阻止默认效果
                            updateWorkFlowStatusCriterion(false, function (data) {
                                $("#dialog-criterion-form").dialog("close"); //关闭窗口
                            }, function (data) {
                                showMessage("新增关卡", data.msg, false);
                            },lastStatusId)
                        },
                        "取消": function (e) {
                            $("#dialog-criterion-form").dialog("close"); //关闭窗口
                        }
                    }
                });
            })
        }

        $(".criterion-add").click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            if(lastStatusId == -1){
                $.alertable.alert("请先选择关卡");
                return;
            }
            $("#dialog-criterion-form").dialog({
                model: true,
                title: "新增关卡标准",
                open: function (event, ui) {
                    $(".ui-dialog-titlebar-close", $(this).parent()).hide();
                    var currentWorkFlowStatus = statusMap[lastStatusId];
                    optionStr = Mustache.render("<option value='{{id}}'>{{name}}</option>", {id: currentWorkFlowStatus.id, name: currentWorkFlowStatus.name});
                    $("#criterionForm")[0].reset();
                    $("#statusParentId").html(optionStr);
                    //$("#parentId").attr("disabled","disabled");
                },
                buttons: {
                    "添加": function (e) {
                        e.preventDefault(); //阻止默认效果
                        updateWorkFlowStatusCriterion(true, function (data) {
                            $("#dialog-criterion-form").dialog("close"); //关闭窗口
                        }, function (data) {
                            showMessage("新增关卡", data.msg, false);
                        },lastStatusId)
                    },
                    "取消": function (e) {
                        $("#dialog-criterion-form").dialog("close"); //关闭窗口
                    }
                }
            });
        })

        function updateWorkFlowStatusCriterion(isCreate, successCallback, failCallback, lastStatusId) {
            $.ajax({
                url: isCreate ? "plm/workFlowStatusCriterion/save.json" : "plm/workFlowStatusCriterion/update.json",
                data: $('#criterionForm').serializeArray(),
                type: 'POST',
                success: function (result) {
                    if(result.ret) {
                        //成功后渲染
                        loadWorkFlowStatusCriterionList(lastStatusId);
                        if (successCallback) {
                            successCallback(result);
                        }
                    }else {
                        if (failCallback) {
                            failCallback(result);
                        }
                    }
                }
            })
        }

    })

</script>

</body>
</html>
